package service.Impl;

import dao.*;
import entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.jinhuo;
import pojo.stock;
import service.StockDao;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class StockDaoImpl implements StockDao {
    @Autowired
   private IphonetypeDao iphonetypeDao;
    @Autowired
    private dao.StockDao stockDao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private auditDao auditDao;
    @Autowired
    private dao.reviewDao reviewDao;
    @Autowired
    private godownDao godownDao;
    @Autowired
    private godownDetailsDao godownDetailsDao;
    @Override
    public void addstockDao(stock stock) {
        Iphonetype iphonetype=iphonetypeDao.flagType(stock.getTypeName());//取出商品类别名称判断该表中是否有值
        Shop shop=new Shop();//创建一个供货商商店实体类
        if(iphonetype!=null){
            shop.setIptypeid(iphonetype.getIptypeid());
        }else{
            iphonetypeDao.addType(stock.getTypeName());
            Iphonetype iphonetype1=iphonetypeDao.showaddType();
            shop.setIptypeid(iphonetype1.getIptypeid());
        }
        shop.setFirmId(stock.getFirmIds());
        shop.setShopcolor(stock.getShopColor());
        shop.setShopprice(stock.getShopPrice());
        shop.setShopType(stock.getShopType());
        shop.setShopName(stock.getShopName());
        shopDao.addshop(shop);//往供货商商店添加商品
        Shop shop1=shopDao.showaddshop();
        Stock stock1=new Stock();
        stock1.setShopId(shop1.getShopid());
        stock1.setStockNum(stock.getStockNum());
        stock1.setStockTotal(stock.getStockNum()*stock.getShopPrice());
        stockDao.addStocks(stock1);//往进货单中添加商品
    }

    @Override
    public void caigodan(jinhuo jinhuo, HttpServletRequest request) {
        //生成12位随机数
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("YYMMddHHmmss");
        String data=df.format(day);//生成订单号
        String dindan="CG"+data;//生成销售订单号
        Audit audit=new Audit();
        //取出正在登录的管理员作为经办人的编号
        int Aid=(int)request.getSession().getAttribute("Aid");
        audit.setAid(Aid);
        audit.setSeat(dindan);
        audit.setPresetPrice(jinhuo.getTotal());
        audit.setPrice(jinhuo.getPrice());
        audit.setSums(jinhuo.getNum());
        auditDao.addAudit(audit);
        List<Stock> list=stockDao.showStatestock(2);
        for (Stock stock:list){
            Review review=new Review();
            review.setSeat(dindan);
            review.setShopId(stock.getShopId());
            review.setReviewNum(stock.getStockNum());
            review.setReviewTotal(stock.getStockTotal());
            reviewDao.addreview(review);
        }
    }

    @Override
    public void ruku(String seat,HttpServletRequest request) {
        //取出正在登录的管理员所有信息
        Administrator administrator=(Administrator) request.getSession().getAttribute("ad");
        auditDao.upStateid(seat,administrator.getAname());//采购进行审核
        Godown godown=new Godown();
        godown.setSeat(seat);
        godown.setStateid(2);
        godownDao.addGoDown(godown);//添加入库审核单
        List<Review> list=reviewDao.showReviews(seat);//根据单号查询采购详情单中的详情信息
        for (Review review:list){
            Godowndetails godowndetails=new Godowndetails();
            godowndetails.setSeat(seat);//采购单号
            godowndetails.setShopId(review.getShop().getShopid());//厂商商品编号
            godowndetails.setGdNum(review.getReviewNum());//本商品应购买数量
            godowndetails.setGdNums(review.getReviewNum());//本商品实购买数量
            godowndetails.setGdtotal(review.getReviewTotal());//本商品应付款金额
            godowndetails.setGdtotals(review.getReviewTotal());//本商品实付款金额
            godownDetailsDao.addGoDownDetails(godowndetails);//添加入库审核单中的详情信息
        }
    }
}
